package com.insigma.business.qrcode.soldier._provider.impl;


import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.LogUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dao.SoldierOuterLoginAccountDao;
import com.insigma.business.qrcode.soldier.dto.SoldierOuterLoginAccountDTO;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.soldier.entity.SoldierOuterLoginAccountDO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.request.SoldierOuterLoginAccountBSearcher;
import com.insigma.business.qrcode.soldier.request.SoldierOuterLoginAccountInsertRequest;
import com.insigma.business.qrcode.soldier.request.SoldierOuterLoginAccountSearchRequest;
import com.insigma.business.qrcode.soldier._provider.BaseSoldierOuterLoginAccountProvider;
import com.insigma.business.qrcode.soldier.service.SoldierOuterLoginAccountService;
import com.insigma.business.qrcode.common.converter.SoldierOuterLoginAccountProviderConverter;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;

@Slf4j
@Service()
@Component("baseSoldierOuterLoginAccountProvider")
public class BaseSoldierOuterLoginAccountProviderImpl extends BaseProviderImpl implements BaseSoldierOuterLoginAccountProvider {

    @Autowired(required = false)
    private SoldierOuterLoginAccountDao soldierOuterLoginAccountDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    @Autowired
    private SoldierOuterLoginAccountService soldierOuterLoginAccountService;

    @Override
    public BaseResponse<SoldierOuterLoginAccountDTO> getByOuterSysTypeAndUserId(Integer outerSystemType, String outerSystemAccountId) {
        LogUtil.info(log, "获取单条SoldierOuterLoginAccount，请求参数:", outerSystemType, outerSystemAccountId);

        if (Strings.isBlank(outerSystemAccountId)) {
            return ResponseUtils.buildSuccessResponse(null);
        }

        SoldierOuterLoginAccountDO byOuterSysTypeAndUserId = soldierOuterLoginAccountService.getByOuterSysTypeAndUserId(outerSystemType, outerSystemAccountId);
        final SoldierOuterLoginAccountDTO remoteObj = SoldierOuterLoginAccountProviderConverter.convertResponseDO2DTO(byOuterSysTypeAndUserId);

        return ResponseUtils.buildSuccessPagingResponse(remoteObj);
    }

    @Override
    public BaseResponse<SoldierOuterLoginAccountDTO> getByIdCardNo(String idCardNo) {
        SoldierOuterLoginAccountDO byOuterSysTypeAndUserId = soldierOuterLoginAccountService.getByIdCardNo(idCardNo);
        final SoldierOuterLoginAccountDTO remoteObj = SoldierOuterLoginAccountProviderConverter.convertResponseDO2DTO(byOuterSysTypeAndUserId);

        return ResponseUtils.buildSuccessPagingResponse(remoteObj);
    }

    @Override
    public BaseResponse<Integer> deleteSoldierOuterLoginAccount(BaseDeleteRequest deleteRequest) {
        LogUtil.info(log, "逻辑删除SoldierOuterLoginAccount，请求参数:", deleteRequest);
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除SoldierOuterLoginAccount的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final Integer affectRowNum = soldierOuterLoginAccountService.delete((String) deleteRequest.getId(), deleteRequest.getModifierType(), (String) deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_OUTER_LOGIN_ACCOUNT_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<SoldierOuterLoginAccountDTO> getByZlbId(String outerZlbAccountId) {
        SoldierOuterLoginAccountDO byOuterSysTypeAndUserId = soldierOuterLoginAccountService.getByZlbId(outerZlbAccountId);
        final SoldierOuterLoginAccountDTO remoteObj = SoldierOuterLoginAccountProviderConverter.convertResponseDO2DTO(byOuterSysTypeAndUserId);

        return ResponseUtils.buildSuccessPagingResponse(remoteObj);

    }

    @Override
    public BasePagingResponse<List<SoldierOuterLoginAccountDTO>> searchSoldierOuterLoginAccount(SoldierOuterLoginAccountSearchRequest searchRequest) {
        LogUtil.info(log, "查询分页SoldierOuterLoginAccount，请求参数:", searchRequest);
        final BasePagingResponse<List<SoldierOuterLoginAccountDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页SoldierOuterLoginAccount的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final SoldierOuterLoginAccountBSearcher soldierOuterLoginAccountBSearcher = SoldierOuterLoginAccountProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<SoldierOuterLoginAccountDO> soldierOuterLoginAccountDOList = soldierOuterLoginAccountService.search(soldierOuterLoginAccountBSearcher);
                final List<SoldierOuterLoginAccountDTO> soldierOuterLoginAccountDTOList = SoldierOuterLoginAccountProviderConverter.convertResponseDOList2DTOList(soldierOuterLoginAccountDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, soldierOuterLoginAccountDTOList, searchRequest, soldierOuterLoginAccountBSearcher.getTotalCount());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_OUTER_LOGIN_ACCOUNT_SEARCH;
            }
        });

        return basePagingResponse;
    }

    @Override
    public Boolean updateRealNameIfMobileExisted(String soldierId, String mobile, String userName) {
        return soldierOuterLoginAccountDao.updateRealNameIfMobileExisted(soldierId, mobile, userName);
    }

    @Override
    public BaseResponse<SoldierOuterLoginAccountDTO> getSoldierOuterLoginAccount(BaseGetRequest baseGetRequest) {
        LogUtil.info(log, "获取单条SoldierOuterLoginAccount，请求参数:", baseGetRequest);
        final BaseResponse<SoldierOuterLoginAccountDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条SoldierOuterLoginAccount的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final SoldierOuterLoginAccountDO soldierOuterLoginAccountDO = soldierOuterLoginAccountService.get((String) baseGetRequest.getId());
                final SoldierOuterLoginAccountDTO remoteObj = SoldierOuterLoginAccountProviderConverter.convertResponseDO2DTO(soldierOuterLoginAccountDO);
                ResponseUtils.setupSuccessResponse(baseResponse, remoteObj);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_OUTER_LOGIN_ACCOUNT_GET;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> insertSoldierOuterLoginAccount(SoldierOuterLoginAccountInsertRequest insertRequest) {
        LogUtil.info(log, "新增SoldierOuterLoginAccount，请求参数:", insertRequest);
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增SoldierOuterLoginAccount的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final SoldierOuterLoginAccountDO soldierOuterLoginAccountDO = SoldierOuterLoginAccountProviderConverter.convertRequestIO2DO(insertRequest);
                Integer insert = soldierOuterLoginAccountService.insert(soldierOuterLoginAccountDO);
                ResponseUtils.setupSuccessResponse(baseResponse, insert);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_OUTER_LOGIN_ACCOUNT_INSERT;
            }
        });
        return baseResponse;
    }
}
